<?php
/**
 * @author sanketsp
 * Survey class: Holds data on each survey created by the instructor
 *
 */
class Survey {
	/**
	 * Constructor
	 * @param $id ID of the survey
	 */
	function __construct($id) {
		$query = mysql_query("SELECT * FROM `survey` WHERE `id` = '$id'") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			foreach($result as $key=>$value) {
				$this->$key = $value;
			}
		}
	}
	/**
	 * Updates the survey class
	 */
	public function updateSurvey($data){
		$sql = mysql_query("UPDATE `survey` SET
			`survey_name` = '$data->survey_name',
			`survey_title` = '$data->survey_title',
			`welcome_message` = '$data->welcome_message',
			`confirmation_message` = '$data->confirmation_message',
			`email_subject` = '$data->email_subject',
			`email_message`= '$data->email_message',
			`last_modified`= '$data->last_modified'
			WHERE `id`='$this->id'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns all questions belonging to this survey
	 */
	public function getAllQuestions() {
		$query = mysql_query("SELECT * FROM `question` WHERE `survey_id` = '$this->id' ORDER BY `id`") or die(mysql_error());
		while($r = mysql_fetch_object($query)) {
			$questions[] = new Question($r->id);
		}
		if(isset($questions)) {
			return $questions;
		} else {
			return false;
		}
	}
	/**
	 * Returns a single question created for this survey
	 * @param $id requires the question id
	 */
	public function getQuestion($id) {
		$query = mysql_query("SELECT * FROM `question` WHERE  `id` = $id") or die(mysql_error());
		$result = mysql_fetch_object($query);
		if($result) {
			return new Question($result->id);
		} else {
			return false;
		}
	}
	/**
	 * Adds a new question to this survey
	 * @param $data data string required
	 */
	public function addQuestion($data) {
		$query = mysql_query("INSERT INTO `question` VALUES (
			NULL,
			'$this->id',
			'$data->type',
			'$data->required',
			'$data->question',
			'$data->create_section',
			'$data->order',
			'$data->response1',
			'$data->response2',
			'$data->response3',
			'$data->response4',
			'$data->response5',
			'$data->response6')")
		or die(mysql_error());
	}
	/**
	 * Deletes a question from this survey
	 * @param $id requires the question id
	 */
	public function deleteQuestion($id) {
		$sql = mysql_query("DELETE FROM `question` WHERE  `id` = '$id'") or die(mysql_error());
		return true;
	}
	/**
	 * Returns the total number of responses made to this survey
	 */
	public function getTotalNumberOfResponses() {
		$sql = "SELECT * FROM `response` WHERE `survey_id` = '$this->id'";
		$result = mysql_query($sql) or die("getting number of responses".mysql_error());
		$total_response = mysql_num_rows($result);
		if($total_response) {
			return $total_response;
		} else {
			return false;
		}
	}
	/**
	 * Returns all conditional queries made for questions belonging to this survey
	 */
	public function getAllConditions() {
		$sql = "SELECT * FROM `branching` WHERE `survey_id` = '$this->id'";
		$result = mysql_query($sql)or die("Check Condition:".mysql_error());
		$i = 0;
		while($c = mysql_fetch_object($result)) {
			$condition[$i]['id'] = $c->id;
			$condition[$i]['question_1_id'] = $c->question_1_id;
			$condition[$i]['question_2_id'] = $c->question_2_id;
			$condition[$i]['condition']=$c->condition;
			$condition[$i]['condition_value']= $c->value;
			$i++;
		}
		if($condition) {
			return $condition;
		} else {
			return false;
		}
	}
}
?>